<template>
  <div class="upload_demo">
    <el-upload
      class="upload-demo"
      action="#"
      :before-upload="handleBeforeUpload"
      :on-remove="remove"
      :drag="true"
      :on-change="result"
      :http-request="uploadFile"
      :limit="limit"
      :on-exceed="handleExceed"
      :accept="accept"
      :on-preview="clickDwo"
      :disabled="disabled"
      :file-list="fileList | dataFilter"
    >
      <!-- 上传按钮 -->
      <div
        class="el-upload__tip"
        slot="tip"
      >
        <!-- <template v-if="fileSize">
          大小不超过 <b
            v-if="fileSize < 1000"
            style="color: #f56c6c"
          >{{ fileSize }}MB</b>
          <b
            v-else
            style="color: #f56c6c"
          >{{ fileSize / 1000 }}GB</b>
        </template> -->
        <template v-if="accept">
          <div style="height: 36px; display: flex; align-items: center;">
            格式为 <b style="color: #f56c6c">{{ accept }}</b>
          文件
          </div>
        </template>
      </div>
      <el-button
        type="primary"
      >选取文件</el-button>
    <!-- <i class="el-icon-upload"></i>
      <div class="el-upload__text"><em>点击上传</em> / 拖拽到此区域</div> -->
    <!-- 上传提示 -->
    </el-upload>
    <!-- <el-upload
      class="upload-demo"
      drag >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
    </el-upload> -->
  </div>
</template>
<script>
export default {
  name: "FUpload",
  props: {
    // 值
    fileList: {
      type: Array | Object,
      default: function () {
        return [];
      },
    },
    // 数量限制
    limit: {
      type: Number,
      default: 99,
    },
    // 大小限制(MB)
    fileSize: {
      type: Number,
      default: 1000,
    },
    // 文件类型
    fileType: {
      type: Array,
      default: () => ["pdf", "doc", "docx", "xls", "xlsx"],
    },
    // 文件类型,
    accept: {
      type: String,
      default: "*",
    },
    // 是否禁用,
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  filters: {
    // 处理 type, 文件名
    dataFilter(item) {
      if (item) {
        let type = Object.prototype.toString.call(item) === "[object Object]";
        if (type) {
          return [{ ...item, name: item.originalName || item.name || item.fileName }];
        } else {
          return item.map((e) => ({ ...e, name: e.originalName || e.name || e.fileName }));
        }
      } else {
        return [];
      }
    },
  },
  methods: {
    // 点击文件
    clickDwo(file) {
      let path = file.path || file.filePath || file.previewPath;
      let name = file.name || file.fileName || file.originalName;
      if (path) {
        this.$download.preview(path, name);
      } else {
        this.$modal.msgWarning("请上传成功后再预览文件");
      }
    },
    result(file, all) {
      this.$emit("change", [file, all]);
      this.$emit("update:fileList", all);
    },
    remove(file, all) {
      this.$emit("remove", [file, all]);
      this.$emit("update:fileList", all);
    },
    // 绑定在http-request,覆盖上传事件
    uploadFile(file) {
      this.$emit("upload", file);
    },
    handleExceed() {
      this.$message.error(`上传文件数量不能超过 ${this.limit} 个!`);
    }, // 上传前校检格式和大小
    handleBeforeUpload(file) {
      // 校检文件类型
      // if (this.fileType) {
      //   let fileExtension = "";
      //   if (file.name.lastIndexOf(".") > -1) {
      //     fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
      //   }
      //   const isTypeOk = this.fileType.some((type) => {
      //     if (file.type.indexOf(type) > -1) return true;
      //     if (fileExtension && fileExtension.indexOf(type) > -1) return true;
      //     return false;
      //   });
      //   if (!isTypeOk) {
      //     this.$message.error(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`);
      //     return false;
      //   }
      // }
      // 校检文件大小
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize;
        if (!isLt) {
          this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`);
          return false;
        }
      }
      return true;
    },
  },
};
</script>
<style lang="scss">
.el-upload__tip {
  margin-top: 0;
}
.el-upload-dragger {
  margin-right: 10px;
  width: auto !important;
  height: auto !important;
  .el-button {
    padding: 10px !important;
  }
}
.el-upload {
  float: left;
}
</style>
